var app2 = angular.module('app1', ['emailParser']);
var app=angular.module('app1');
app.controller('MyController', ['$scope', '$parse', function($scope, $parse) {
    //解析Angularjs表达式
    $scope.$watch('expr', function(newVal, oldVal, scope) {
        if (newVal !== oldVal) {
            // Let's set up our parseFun with the expression
            var parseFun = $parse(newVal);
            // Get the value of the parsed expression, set it on the scope for output
            scope.parsedExpr = parseFun(scope);
        }
    });
    //解析Angularjs表达式
    $scope.$watch('expr2', function(newVal, oldVal, scope) {
        if (newVal !== oldVal) {
            // Let's set up our parseFun with the expression
            console.log("旧值=" + oldVal + ",新值=" + newVal);
            //var parseFun = $parse(newVal);
            //console.log(parseFun);
            // Get the value of the parsed expression, set it on the scope for output
            scope.parsedExpr2 = newVal;
        }
    });
}]);
//插值字符串
app.controller('MyController2', ['$scope', '$interpolate', function($scope, $interpolate) {
    $scope.to2 = '';
    $scope.emailBody = 'Hello {{ to2 }}\n\n';
    // Set up a watch
    $scope.$watch('emailBody2', function(body) {
        if (body) {
            var template = $interpolate(body);
            $scope.previewText =
                template({ to2: $scope.to2 });
        }
    });
}]);

//插值字符串
app.controller('MyController3', ['$scope','EmailParser', function($scope,EmailParser) {
    $scope.to = 'ari@fullstack.io';
    $scope.emailBody = 'Hello __to__';
    // Set up a watch
    $scope.$watch('emailBody', function(body) {
        if (body) {
            $scope.previewText =
                EmailParser.parse(body, {
                    to: $scope.to
                });
        }
    });
}]);

angular.module('emailParser', [])
.config(['$interpolateProvider',
    function($interpolateProvider) {
        $interpolateProvider.startSymbol('__');
        $interpolateProvider.endSymbol('__');
    }
])
.factory('EmailParser', ['$interpolate',
    function($interpolate) {
        // a service to handle parsing
        return {
            parse: function(text, context) {
                var template = $interpolate(text);
                return template(context);
            }
        };
    }
]);
